﻿/*************************************************************************
 * Created by OhZee Creative, LLC
 * 2013
 * This code is licensed under Microsoft Public License (Ms-PL)
 * http://opensource.org/licenses/ms-pl
 * http://ozframework.codeplex.com/
 *************************************************************************/

namespace OhZeeCreative
{
    using System;
    using System.Windows.Input;

	public abstract class DelegateCommandBase : ICommand
	{
		private bool isEnabled = true;
		public bool IsEnabled
		{
			get { return this.isEnabled; }
			set
			{
				if (this.isEnabled != value)
				{
                    this.isEnabled = value;
					if (this.CanExecuteChanged != null)
					{
						this.CanExecuteChanged(this, EventArgs.Empty);
					}
				}
			}
		}

		public bool CanExecute(object parameter)
		{
			return this.IsEnabled;
		}

		public event EventHandler CanExecuteChanged;

		public abstract void Execute(object parameter);
	}

	public class DelegateCommand : DelegateCommandBase
	{
		private Action action;

		public DelegateCommand(Action action)
		{
			this.action = action;
		}

		public override void Execute(object parameter)
		{
			if (this.CanExecute(parameter))
			{
				this.action();
			}
		}
	}

	public class DelegateCommand<T> : DelegateCommandBase
	{
		private Action<T> action;

		public DelegateCommand(Action<T> action)
		{
			this.action = action;
		}

		public override void Execute(object parameter)
		{
			if (this.CanExecute(parameter))
			{
				this.action((T) parameter);
			}
		}
	}
}
